using System;
using System.Collections.Generic;
using System.Text;

namespace Firestarter.DB.Schema.Logic
{
    public class DBUtilities
    {
        public static int? ConvertDBToInt(object rowDBValue)
        {
            return Convert.ToInt32(ConvertDBNullToNull(rowDBValue));
        }

        public static string ConvertDBToString(object rowDBValue)
        {
            object value = ConvertDBNullToNull(rowDBValue);
            if (value == null) return null;
            return Convert.ToString(value);
        }

        public static object ConvertDBNullToNull(object value)
        {
            if (value is DBNull) return null;
            return value;
        }

        public static DateTime? ConvertDBNullToNullDateTime(object value)
        {
            if (value is DBNull) return null;
            return Convert.ToDateTime(value);
        }

        public static bool ConvertDBToBoolean(object value)
        {
            return ConvertToBoolean(ConvertDBNullToNull(value));
        }

        public static bool ConvertToBoolean(object value)
        {
            if (value is string)
            {
                string stringValue = value as string;
                switch (stringValue.ToUpper())
                {
                    case "TRUE":
                    case "T":
                    case "YES":
                    case "Y":
                    case "1":
                        return true;
                    case "FALSE":
                    case "F":
                    case "NO":
                    case "N":
                    case "0":
                        return false;
                    default:
                        return Convert.ToBoolean(stringValue);
                }
            }
            return Convert.ToBoolean(value);
        }
    }
}
